package com.blkbox.javaee.quartz.multipletrigger;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class MultipleTriggerExample {

	public static void main(String[] args) throws Exception {
		JobDataMap dataMap = new JobDataMap();
		
		JobKey jobKeyA = new JobKey("jobA", "group1");
		JobDetail jobA = JobBuilder.newJob(OneJob.class).withIdentity(jobKeyA).usingJobData(dataMap).build();

		JobKey jobKeyB = new JobKey("jobB", "group1");
		JobDetail jobB = JobBuilder.newJob(OneJob.class).withIdentity(jobKeyB).usingJobData(dataMap).build();

		JobKey jobKeyC = new JobKey("jobC", "group1");
		JobDetail jobC = JobBuilder.newJob(OneJob.class).withIdentity(jobKeyC).usingJobData(dataMap).build();

		Trigger triggerA = TriggerBuilder.newTrigger().withIdentity("dummyTriggerName1", "group1")
				.withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build();

		Trigger triggerB = TriggerBuilder.newTrigger().withIdentity("dummyTriggerName2", "group1")
				.withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?")).build();

		Trigger triggerC = TriggerBuilder.newTrigger().withIdentity("dummyTriggerName3", "group1")
				.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();

		Scheduler scheduler = new StdSchedulerFactory().getScheduler();

		scheduler.start();
		scheduler.scheduleJob(jobA, triggerA);
		scheduler.scheduleJob(jobB, triggerB);
//		scheduler.scheduleJob(jobC, triggerC);
		scheduler.pauseJob(triggerA.getJobKey());
		Thread.sleep(10000);
		scheduler.resumeJob(jobKeyA);
//		Thread.sleep(6000);
//		scheduler.unscheduleJob(triggerB.getKey());
		Thread.sleep(5000);
		scheduler.rescheduleJob(triggerB.getKey(), triggerC);
		Thread.sleep(5000);
		scheduler.deleteJob(jobKeyA);
	}

}
